package com.study.leetcode;

import javax.swing.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @description: zimuyiweici   字母异位词
 * @date: 2020/11/22 20:32
 * @author: hcm
 * @version: 1.0
 */
public class zimuyiweici {
    public static void main(String[] args) {
        String s = "a";
        String t = "b";
        boolean anagram = isAnagram(s, t);
        System.out.println(anagram);
    }

    public static boolean isAnagram(String s, String t) {
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();

        HashMap mapTc = new HashMap();
        HashMap mapSc = new HashMap();
        /**
         * 长度判断
         */
        if(s.length()!= t.length()){
            return false;
        }
        for (char n : tc) {
         if(mapTc.containsKey(n)){
             int count = (int) mapTc.get(n);
             count ++ ;
             mapTc.put(n,count);
         }else {
             mapTc.put(n,1);
         }
        }
        for (char n : sc) {
            if(mapSc.containsKey(n)){
                int count = (int) mapSc.get(n);
                count ++ ;
                mapSc.put(n,count);
            }else {
                mapSc.put(n,1);
            }
        }
        for (char c : tc) {
            if(mapSc.get(c) == null){
                return false;
            }
            if((int)mapTc.get(c)!=(int)mapSc.get(c)){
                return false;
            }
        }
        return true;
    }
}
